//
//  SecendViewController.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#import "SecendViewController.h"
#import "ZbHeadView.h"
#import "NetworkHandlerBlock.h"
#import "ModelForSecendVC.h"
#import "CellForContent.h"
#import "ZbTableViewCustomHeadView.h"
#import "ItemVC3.h"

#import "UIImageView+WebCache.h"

#import "PlayerViewController.h"
#import "DataBaseHandler.h"
#import "ModelForListen.h"

@interface SecendViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSString *stringForCoverLarge;
@property (nonatomic, retain) NSString *stringForCoverSmall;
@property (nonatomic, retain) NSString *stringForTitle;

@property (nonatomic, retain) NSMutableDictionary *dicForAlbumId;
@property (nonatomic, retain) NSMutableArray *arrForList;
@property (nonatomic, retain) UIImageView *backGroundImage;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UIImageView *imageBackGround;
@property (nonatomic, retain) UIView *visualView;

@property (nonatomic, retain) NSArray *arrForListen;

@property (nonatomic, assign) BOOL isOk;


@end

@implementation SecendViewController

- (void)dealloc {

    [_stringForCoverLarge release];
    [_stringForCoverSmall release];
    [_stringForTitle release];
    [_dicForAlbumId release];
    [_arrForList release];
    [_backGroundImage release];
    [_tableView release];
    [super dealloc];

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];
    [self.tableView reloadData];

}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    // 隐藏navigationBar.
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self handleData];
}


- (void)handleData {
    
    self.dicForAlbumId = [NSMutableDictionary dictionary];
    self.arrForList = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/20?position ",self.stringForAlbumID];
    
    [NetworkHandlerBlock netWorkingWithURL:str completeHandle:^(NSURLResponse *response, NSData *data, id result) {

        self.dicForAlbumId    = [result objectForKey:@"album"];
        NSDictionary *dicTemp = [result objectForKey:@"tracks"];
        
        NSArray *arrTemp = [dicTemp objectForKey:@"list"];
        for (NSDictionary *dic in arrTemp) {
            ModelForSecendVC *model = [[ModelForSecendVC alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForList addObject:model];
        }
     
        [self createZbView];
       [self createTableView];
 
    }];
}


#pragma mark createZbView
- (void)createZbView {

    // 毛玻璃视图层.
    self.imageBackGround = [[UIImageView alloc]init];
    self.imageBackGround.frame = self.view.bounds;
    self.imageBackGround.userInteractionEnabled = YES;
    
//    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:2];
//    self.visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
//    self.visualView.frame = self.view.bounds;
//    [self.imageBackGround addSubview:self.visualView];
//    [self.visualView release];
    
    // 得到网络加载的背景图片.
    NSString *str = [self.dicForAlbumId objectForKey:@"coverSmall"];
    [self.imageBackGround sd_setImageWithURL:[NSURL URLWithString:str]];
    [self.imageBackGround release];
    
    ZbHeadView *zbHeadView = [[ZbHeadView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:zbHeadView];
    [zbHeadView passDataWith:self.dicForAlbumId];
    [zbHeadView release];
    
}

#pragma mark createTableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 200) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    
//    self.tableView.backgroundColor = [UIColor colorWithPatternImage:nil];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    // 隐藏tableView右侧的滚动条.
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[CellForContent class] forCellReuseIdentifier:@"poolForCellForContent"];
    
    [self.tableView release];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

     ZbTableViewCustomHeadView *zbTableHeadView = [[ZbTableViewCustomHeadView alloc]init];
    zbTableHeadView.model = self.modelForListen;

    [zbTableHeadView passCountWithModel:self.arrForList];
    
        [zbTableHeadView.buttonForCollect setTitle:@"收藏" forState:UIControlStateNormal];
        [zbTableHeadView.buttonForCollect setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
        NSArray *arr = [[DataBaseHandler shareDataBaseHandler]selectForModel];
        for (ModelForListen *model in arr) {
    
    // 注:此处若写在创建该button的地方.self.model.title会一直为空.因为该视图的初始化时.值还没有传到model.title里.
        if ([self.modelForListen.title isEqualToString:model.title]) {
    
            [zbTableHeadView.buttonForCollect setTitle:@"已收藏" forState:UIControlStateNormal];
             zbTableHeadView.buttonForCollect.titleLabel.font = [UIFont systemFontOfSize:24];
            [zbTableHeadView.buttonForCollect setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }
        }
    return zbTableHeadView;
}


- ( CGFloat )tableView:( UITableView  *)tableView heightForHeaderInSection:( NSInteger )section {
    return 100;
}

#pragma mark tableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrForList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForContent *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCellForContent"];
    ModelForSecendVC *model = [self.arrForList objectAtIndex:indexPath.row];
    [cell passModelForCell:model];
    return cell;
}

#pragma mark tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ModelForSecendVC *model = [self.arrForList objectAtIndex:indexPath.row];

    ItemVC3 *player = [[ItemVC3 alloc]init];
    player.musicUrlString = model.playUrl64;
    player.model = model;
    player.arrForModel = self.arrForList;
    player.index = indexPath.row;
    
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:player];
    [self presentViewController:navi animated:YES completion:^{
    }];
}


- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}


@end
